<?php $this->load->view('includes/head.php'); ?>

<?php // var_dump($tasks); ?>
<!-- </pre> -->
<div class="container">
  <div class="row">
    <div class="col-sm-12">

      <h3>Tasks <small class="pull-right"><a href="<?php echo site_url('/tasks/add-new-task'); ?>">Create Task</a></small></h3>
      <hr>
      <?php if (count($tasks) < 1): ?>
        <div class="alert alert-warning" role="alert"><p>You currently do not own any tasks.</p></div>
      <?php else: ?>
      <table class="table table-hover table-striped">
        <thead>
          <tr>
            <th class="manage-row-1">#</th>
            <th class="manage-row-2">Task</th>
            <th class="manage-row-3">RACI Role(s)</th>
            <th class="manage-row-4">Priority</th>
            <th class="manage-row-5">Due Date</th>
            <th class="manage-row-6">Action</th>
          </tr>
        </thead>
        <tbody>
          <?php
          $count = 0;
          date_default_timezone_set('GMT');

  
            foreach ($tasks as $task) {

              $title = $task->title;
              $url = site_url('tasks/'.$task->url);

              $view_task_url = site_url('tasks/'.$task->url);
              $delete_task_url = site_url('tasks/delete/'.$task->url);
              $edit_task_url = site_url('tasks/edit/'.$task->url);
              $content = $task->content;
              $count++;
              $due_date = mdate('%M %d, %Y',strtotime($task->date_due));
              $due = date('Y-m-d',strtotime($task->date_due));
/*
              if (is_array(json_decode($task->responsible))) {
                $responsible = (in_array($task->user_id, json_decode($task->responsible))) ? '<em><span class="levels">Responsible</span></em> ' : false;
              }elseif (is_array(json_decode($task->accountable))) {
                $accountable = (in_array($task->user_id, json_decode($task->accountable))) ? '<em><span class="levels">Accountable</span></em> ' : false;
              }elseif (is_array(json_decode($task->consulted))) {
                $consulted = (in_array($task->user_id, json_decode($task->consulted))) ? '<em><span class="levels">Consulted</span></em> ' : false;
              }elseif (is_array(json_decode($task->informed))) {
                $informed = (in_array($task->user_id, json_decode($task->informed))) ? '<em><span class="levels">Informed</span></em> ' : false;
              }
*/
              $priority;
              switch ($task->priority) {
                  case '1':
                    $priority = '<span class="label label-default">Low</span>';
                    break;
                  case '2':
                    $priority = '<span class="label label-warning">Normal</span>';
                    break;
                  case '3':
                    $priority = '<span class="label label-danger">High</span>';
                    break;
                }
              $action;
              if ($user_id == $task->user_id) {
                $action = '<li><a href="'.$delete_task_url.'" rel="tooltip" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="right" title="Delete Task"><i class="fa fa-times"></i></a></li>';
              }else{
                $action = '';
              }

              echo<<<TSK
              <tr>
                <td class="manage-row-1">$count</td>
                <td class="manage-row-2">
                  <strong>$title</strong>
                </td>
TSK;
              echo '<td class="table-roles">';
              $responsible = json_decode($task->responsible);
              $accountable = json_decode($task->accountable);
              $consulted = json_decode($task->consulted);
              $informed = json_decode($task->informed);
                if (is_array($responsible)) {
                  echo (in_array($task->user_id, $responsible)) ? '<em><span class="levels">Responsible</span></em> ' : '';
                }elseif (is_array($accountable)) {
                  echo (in_array($task->user_id, $accountable)) ? '<em><span class="levels">Accountable</span></em> ' : '';
                }elseif (is_array($consulted)) {
                  echo (in_array($task->user_id, $consulted)) ? '<em><span class="levels">Consulted</span></em> ' : '';
                }elseif (is_array($informed)) {
                  echo (in_array($task->user_id, $informed)) ? '<em><span class="levels">Informed</span></em> ' : '';
                }
              echo '</td>';

              echo<<<BTM
                
                <td class="manage-row-4">$priority</td>
                <td class="manage-row-5">$due_date <em class="time_till_due" data-timestamp="$due"></em></td>
                <td class="manage-row-6">
              <div class="btn-group">
               <ul class="list-inline">
                <li>
                  <a href="$view_task_url" rel="tooltip" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="left" title="View Task"><i class="fa fa-eye"></i></a>
                </li>
                <li>
                  <a href="$edit_task_url" rel="tooltip" class="btn btn-success btn-xs" data-toggle="tooltip" data-placement="top" title="Edit Task"><i class="fa fa-pencil"></i></a>
                </li>
                $action
                </ul>
              </div>
            </td>
              </tr>
BTM;

              }
             
         
          ?>
        </tbody>
      </table>
    <?php endif; ?>
    </div>
  </div>
</div>